<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="__UI__/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="__UI__/css/public.css" media="all">
    <link rel="stylesheet" href="__UI__/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <script type="text/html" id="toolbar">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="reload"> <i class="fa fa-refresh"></i> </button>
            <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add"> 添加 </button>
            <button class="layui-btn layui-btn-sm" lay-event="expand"> 全部展开 </button>
            <button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="fold"> 全部折叠 </button>
        </div>
    </script>

    <table class="layui-hide" id="treetable"  lay-filter="treetable"></table>

</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
</script>

<script src="__UI__/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="__UI__/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        var treeTableRender = function () {
            // 渲染表格
            layer.load(2);
            treetable.render({
                method: 'post', //提交方式
                treeColIndex: 1,
                treeSpid: -1,
                treeIdName: 'id',
                treePidName: 'pid',
                elem: '#treetable',
                toolbar: '#toolbar',
                url: '{:url("admin/menu/index")}',
                page: false,
                cols: [[
                    {type: 'numbers'},
                    {field: 'title', minWidth: 200, title: '菜单名称'},
                    {field: 'icon', title: '图标',templet: function (d) {
                            return d.icon==''? '' : '<i class="'+d.icon+'"></i>';
                        }},
                    {field: 'href', title: '菜单链接'},
                    {field: 'target', title: '打开方式'},
                    {field: 'sort', title: '排序值'},
                    {field: 'status', title: '状态'},
                    {templet: '#auth-state', width: 140, align: 'center', title: '操作'}
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        }

        treeTableRender();

        table.on('toolbar(treetable)', function (obj) {
            if(obj.event === 'reload'){
                treeTableRender();
            }
            else if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '{:url("admin/menu/create")}',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            else if(obj.event === 'expand'){
                treetable.expandAll('#treetable');
            }
            else if(obj.event === 'fold'){
                treetable.foldAll('#treetable');
            }
        });

        //监听工具条
        table.on('tool(treetable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    postSubmit(
                        $,
                        "{:url('admin/menu/delete')}",
                        {id:obj.data.id},
                        function (res) {
                            //请求错误时的处理,currentTableId:是表格的id
                            table.reload('currentTableId');//刷新表格的数据
                        },
                        function () {} //请求错误时的处理
                    );
                    layer.close(index);
                });
                return false;
            } else if (layEvent === 'edit') {
                var index = layer.open({
                    title: '编辑菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '{:url("admin/menu/edit")}?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
        });
    });
</script>
</body>
</html>